/*
 *	Created by Edward on 16/05/03
 *	Copyright (c) 2016 shuwon.com All rights reserved
 */

var yanyang = {
	init: function() {
		switch (jsFun) {
			case 'home':
				yanyang.homePage();
				break;
			case 'about':
				yanyang.aboutPage();
				break;
			case 'contact':
				yanyang.contactPage();
				break;
			case 'jia':
				yanyang.jiaPage();
				break;
			case 'hr':
				yanyang.hrPage();
				break;
			case 'store':
				yanyang.storePage();
				break;
		}
		yanyang.backTop();
	},
	homePage: function() {
		$(".banner ol li").click(function() {
			$(this).addClass("active").siblings().removeClass("active");
			$(".banner ul li").eq($(this).index()).addClass("active").siblings().removeClass("active");
		})
		setTimeout(function() {
			$(".banner ul li").eq(0).addClass("active").siblings().removeClass("active");
			$(".banner ol").stop().animate({
				top: 450,
				opacity: 1
			}, {
				duration: 1600,
				easing: 'easeInOutExpo'
			})
		}, 1500)

		yanyang.flipVideo();
		yanyang.blockScroll();
	},
	aboutPage: function() {
		var mySwiper = new Swiper('.swiper-container', {
			slidesPerView: 3,
			spaceBetween: 25,
			setWrapperSize: true,
			onSlideChangeStart: function(swiper) {
				$("#about .responsibility .listBox .btn").fadeIn();
				if (mySwiper.isBeginning) {
					$("#about .responsibility .listBox .btnPrev").fadeOut();
				}
				if (mySwiper.isEnd) {
					$("#about .responsibility .listBox .btnNext").fadeOut();
				}
			}
		})
		$("#about .responsibility .listBox .btn").click(function() {
			$(this).siblings(".btn").fadeIn();
			if ($(this).hasClass("btnPrev")) {
				mySwiper.slidePrev();
			} else {
				mySwiper.slideNext();
			}
		})
		yanyang.blockScroll();
		yanyang.flipVideo();
	},
	contactPage: function() {
		var map = new BMap.Map("canvers");
		map.enableScrollWheelZoom();
		map.centerAndZoom(new BMap.Point(104.061444, 30.592939), 15);
		marker = new BMap.Marker(new BMap.Point(104.061444, 30.592939));
		map.addOverlay(marker);
	},
	jiaPage: function() {
		var $container = $('.homeList');
		$container.imagesLoaded(function() {
			$container.masonry({
				itemSelector: '.box'
			});
		});
	},
	hrPage: function() {
		$(".r").mCustomScrollbar({
			scrollInertia: 150
		});
	},
	/**
	 * 返回到顶部方法
	 *
	 */
	backTop: function() {
		var offset = 300,
			offset_opacity = 1200,
			scroll_top_duration = 500,
			$back_to_top = $('.backTop');
		$(window).scroll(function() {
			($(this).scrollTop() > offset) ? $back_to_top.addClass('active'): $back_to_top.removeClass('active');
			if ($(this).scrollTop() > offset_opacity) {
				$back_to_top.addClass('active');
			}
		});
		$back_to_top.click(function(event) {
			event.preventDefault();
			$('body,html').animate({
				scrollTop: 0,
			}, scroll_top_duration);
		});
	},
	/**
	 * 视频弹出
	 *
	 */
	flipVideo: function() {
		var video = document.getElementById('myVideo');
		$(".videoBtn").click(function() {
			$("#lightBox").fadeIn(600);
			$(".lightBoxVideo").addClass("active");
			video.play();
		})
		$(".close,.lightBox").click(function() {
			$("#lightBox").fadeOut();
			video.pause();
		})
	},
	/**
	 * 滚动动画
	 *
	 */
	blockScroll: function() {
		$('.block').smoove({
			offset: '10%'
		});
	},
	storePage: function() {
		//需要显示哪一类门店
		storeType = 0;
		var obj;
		//切换
		var aDiv = document.getElementById("list");
		var aDivC;
		var arr = [];
		creatLi(storeType);
		//创建列表的方法
		function creatLi(storeType) {
			//根据类型获取数据
			switch (storeType) {
				case 0:
					obj = storeList0;
					break;

				case 1:
					obj = storeList1;
					break;
				case 2:
					obj = storeList2;
					break;
				case 3:
					obj = storeList3;
					break;
				case 4:
					obj = storeList4;
					break;
				case 5:
					obj = storeList5;
					break;
			}

			//清空列表

			$("#store .changeBox .list").html("").stop(false,true).animate({
				"opacity": "0"
			}, 300).delay(100).stop(false,true).animate({
				"opacity": "1"
			}, 300);

			//创建大图列表
			for (var i = 0; i < obj.samllImg.length; i++) {
				$("#store .changeBox .list").append("<li class='cur" + i + "'><img src='" + obj.samllImg[i] + "' width='100%' ></li>")
			}
			$("#store .changeBox .list li.cur2").css("left", $("#store .changeBox .list").width() - $("#store .changeBox .list li.cur2").width())
				//切换
			aDivC = aDiv.children;
			arr = [];
			for (var i = 0; i < aDivC.length; i++) {
				arr.push([getStyle(aDivC[i], 'left'), getStyle(aDivC[i], 'width'), getStyle(aDivC[i], 'top'), getStyle(aDivC[i], 'zIndex'), getStyle(aDivC[i], 'marginLeft')]);
			}
		}
		//切换
		aDiv = document.getElementById("list");
		arr = [];
		for (var i = 0; i < aDivC.length; i++) {
			arr.push([getStyle(aDivC[i], 'left'), getStyle(aDivC[i], 'width'), getStyle(aDivC[i], 'top'), getStyle(aDivC[i], 'zIndex'), getStyle(aDivC[i], 'marginLeft')]);
		}
		$("#store .changeBox .btn").click(function() {
			var index = $("#store .btnBox ul li.active").index();
			if ($(this).hasClass("btnPrev")) {
				index > 0 ? index-- : index = $("#store .btnBox ul li").length - 1;
				arr.push(arr[0]);
				arr.shift();
			} else {
				index < $("#store .btnBox ul li").length - 1 ? index++ : index = 0;
				arr.unshift(arr[arr.length - 1]);
				arr.pop();
			}
			for (var i = 0; i < aDivC.length; i++) {
				aDivC[i].style.left = arr[i][0];
				aDivC[i].style.width = arr[i][1];
				aDivC[i].style.top = arr[i][2];
				aDivC[i].style.zIndex = arr[i][3];
				aDivC[i].style.marginLeft = arr[i][4];
			}
			$("#store .btnBox ul li").eq(index).addClass("active").siblings().removeClass("active")
		});

		//点击小图
		$("#store .btnBox ul").on("click", "li", function() {
			var index1 = $("#store .btnBox ul li.active").index();
			var index2 = $(this).index();
			if (index2 - index1 > 0) {
				for (var i = 0; i < index2 - index1; i++) {
					$("#store .changeBox .btn.btnNext").click()
				}
			}
			if (index2 - index1 < 0) {
				for (var i = 0; i < index1 - index2; i++) {
					$("#store .changeBox .btn.btnPrev").click()
				}
			}

		});

		function getStyle(obj, attr) {
			if (obj.currentStyle) {
				return obj.currentStyle[attr];
			} else {
				return getComputedStyle(obj, false)[attr];
			}
		}

		var $ul = $("#store .typeBox ul");
		var ul = $ul[0];
		//如果li个数大于3个.那么再复制一遍
		if ($("#store .typeBox ul li").length >= 3) {
			ul.innerHTML += ul.innerHTML;
			//li的个数
			var liNumber = $("#store .typeBox ul li").length;
			//设置ul偏移量
			$ul.css("margin-left", -(liNumber / 2 + storeType - 1) * 350 + "px");

		} else {
			$ul.css("margin-left", "0px")
		}
		//li的个数
		var liNumber = $("#store .typeBox ul li").length;
		$("#store .typeBox ul li").removeAttr("class");
		$("#store .typeBox ul li").eq(storeType + (liNumber / 2)).addClass("active")
		$("#store .typeBox ul li").eq(storeType + (liNumber / 2)).prevAll().addClass("prev");
		$("#store .typeBox ul li").eq(storeType + (liNumber / 2)).nextAll().addClass("next");

		$("#store .typeBox ul li").click(function() {
			var index1 = $("#store .typeBox ul li.active").index();
			var index2 = $(this).index();
			$("#store .typeBox ul li").removeAttr("class");
			$(this).addClass("active");
			$(this).prevAll().addClass("prev");
			$(this).nextAll().addClass("next");
			if (liNumber > 3) {
				index2 -= 1;
				$ul.animate({
					marginLeft: -index2 * 350 + "px"
				});
				//如果点击的是倒数第二个
				if (index2 == liNumber - 3) {
					$("#store .typeBox ul li").removeAttr("class");
					$("#store .typeBox ul li").eq(index2 + 1 - liNumber / 2).addClass("active");
					$("#store .typeBox ul li").eq(index2 + 1 - liNumber / 2).prevAll().addClass("prev");
					$("#store .typeBox ul li").eq(index2 + 1 - liNumber / 2).nextAll().addClass("next");
					$ul.stop().animate({
						marginLeft: -index2 * 350 + "px"
					}, function() {
						$ul.css("margin-left", -(index2 - liNumber / 2) * 350 + "px");
					});
					//如果点击的是第二个
				} else if (index2 == 0) {
					$("#store .typeBox ul li").removeAttr("class");
					$("#store .typeBox ul li").eq(index2 + 1 + liNumber / 2).addClass("active");
					$("#store .typeBox ul li").eq(index2 + 1 + liNumber / 2).prevAll().addClass("prev");
					$("#store .typeBox ul li").eq(index2 + 1 + liNumber / 2).nextAll().addClass("next");
					$ul.stop().animate({
						marginLeft: -index2 * 350 + "px"
					}, function() {
						$ul.css("margin-left", -(index2 + liNumber / 2) * 350 + "px")
					});
				} else {
					$ul.stop().animate({
						marginLeft: -index2 * 350 + "px"
					});
				}
			}
			storeType = $(this).attr("data-type");
			creatLi(storeType * 1);
		})
	}
}
$(function() {
	jsFun = $("#JS").attr('page');
	yanyang.init();
})